<?php

namespace App\Trip\Wechat\Pay;

use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;

class WechatPay
{
    private array|PayConfig $config;
    private Application     $application;

    /**
     * @throws InvalidArgumentException
     */
    public function __construct(array|PayConfig $config = null)
    {
        $this->setConfig($config);
    }

    /**
     */
    public function getMchId()
    {
        return $this->getApplication()->getConfig()->get('mch_id');
    }

    public function getConfig(): array
    {
        return $this->config;
    }

    /**
     * @throws InvalidArgumentException
     */
    public function setConfig(array|PayConfig $config = null): static
    {
        if ($config instanceof PayConfig) {
            $this->config = $config->getConfig();
        } else {
            $this->config = (new PayConfig($config))->getConfig();
        }
        $this->application = new Application($this->config);
        return $this;
    }

    /**
     */
    public function getApplication(): Application
    {
        return $this->application;
    }
}